#!/bin/bash


#This script is a modified version of the original script by Michael Bianco for generating Actionscript language definition files for Xcode.
#It has been modified to generate Xcode language definition files for the haXe programming language (http://www.haxe.org).
#Author: Yariv Sadan

#The original description is below.


#-------- Make AS Def Script -----------------------------------------------------------------------------
#Author: Michael Bianco, <info@mabwebdesign.com>, http://developer.mabwebdesign.com/
#Description:Parses through AS files ending in .as, or .actionscript and pulls out variable, class, and function defitions
#and prepares them for use in xcode. This script is *not* bullet proof, and will include all variables declared with the 'var' keyword in the 
#output file. Because of this the different type of definitions are seperated into differenet temporary files so you can easily include only 
#which definitions you want (I.E. Functions, Variables, Class definitions) by changing the cat command near the end of the script.
#This script only supports AS2 class construction syntax Functions that have no arguments (Ex. doSomething()), are stripped of their parenthesis.
#---------------------------------------------------------------------------------------------------------

#SAMPLE COMMAND: makeXcodeDefFile.bash "/Users/Mike/Library/Application Support/Macromedia/Flash MX 2004/en/Configuration/Classes" ~/Desktop/out.txt

#Pattern comparison, string operators good ref page: http://linuxreviews.org/beginner/abs-guide/en/x2689.html

if (($# < 2)); then
	        echo "You must specify the class directory for the first argument, and a path to the output actionscript def file for the second argument."
		    exit 0
fi

#set the global variables
CLASS_PATH="$1"
DEF_FILE="$2"

#clear the output file if it has already been created
if [ -e "$DEF_FILE" ]; then
 : > $DEF_FILE
fi

#temp def files
CLASS_DEF_FILE=`mktemp`
FUNCTION_DEF_FILE=`mktemp`
VAR_DEF_FILE=`mktemp`

#addition AS language keywords

# haXe note: removed 'intrinsic' from first line and added the last line, for haXe tokens.

ADDITIONAL_KEYWORDS="
import extends implements class interface
public private var static 
super null undefined false true Void this
for while do return new if else switch case break function continue try catch finally
enum Float Int Dynamic Bool package new throw type cast untyped property default extern"


ADD_ADDITIONAL_KEYWORDS=1
ADD_FUNCTION_KEYWORDS=1
ADD_FUNCTION_WITH_ARGS=1

find "$CLASS_PATH" -name *.hx | \
while read path; do
    path=${path/\/\//\/} #get rid of two /'s in the file name
    classDef=`egrep '^(extern )?class [^\{]*' "$path"`
    classDef=`echo $classDef | egrep -o 'class [^ ]*'`
    classDefs=${classDef//class/""} #get rid of 'class '
    for classDef in ${classDefs}; do
	echo "\"$classDef\"," >> $CLASS_DEF_FILE
    done

    classDef=`egrep '^((private|public) )?enum [^\{]*' "$path"`
    classDef=`echo $classDef | egrep -o 'enum [^ ]*'`
    classDefs=${classDef//enum/""} #get rid of 'enum '
    for classDef in ${classDefs}; do
        echo "\"$classDef\"," >> $CLASS_DEF_FILE
    done


    #--------- extract variable definitions ----------
    varDefs=`egrep -o 'var [^:;=]*' "$path"` #sweet, setting multiline output to a single var strips all newlines
    
    #remove all commas & double spaces
    varDefs=${varDefs//,/" "}
    varDefs=${varDefs//"  "/" "}
	    
    for var in $varDefs; do
		len=${#var}
		if [ "$var" != "var" ] && (( ${#var} >= 2 )); then
			echo "\"$var\"," >> $VAR_DEF_FILE
		fi
   done
    
    #extract all function defs
    functionDefs=`egrep -o 'function [^)]+\)' "$path"`
    
    #get rid of all the non-seperating spaces
    functionDefs=${functionDefs//function/}
    functionDefs=${functionDefs//"  "/ }
    functionDefs=${functionDefs//" ,"/,}
    functionDefs=${functionDefs//", "/,}
    functionDefs=${functionDefs//": "/:}
    functionDefs=${functionDefs//"( "/(}
    functionDefs=${functionDefs//" )"/)}
    functionDefs=${functionDefs//" ("/"("}
    functionDefs=${functionDefs//" :"/:}
    functionDefs=${functionDefs//" ->"/->}
    functionDefs=${functionDefs//"-> "/->}
    
    echo "parsing " ${path}
    for funct in $functionDefs; do
#	echo ${funct}
	tempStr=${funct:(-2)} #must put negative index in paranthesis
	if [ "$tempStr" == "()" ]; then #no arguments in the function
	        echo "\"${funct/()/}\"," >> $FUNCTION_DEF_FILE
		    else #there is arguments. group all arguments with <#'s & #>'s
	        funct=${funct/(/"(<#"}
		    funct=${funct/)/"#>)"}
		    funct=${funct//,/"#>,<#"}
		        
		        if [ $ADD_FUNCTION_WITH_ARGS == 1 ]; then
			    echo "\"$funct\"," >> $FUNCTION_DEF_FILE
			    fi
			    
			    if [ $ADD_FUNCTION_KEYWORDS == 1 ]; then
				funct=${funct/(*/} #remove the () part of the function
				        echo "\"$funct\"," >> $FUNCTION_DEF_FILE
					    fi
			        fi
	done
done

cat $FUNCTION_DEF_FILE $VAR_DEF_FILE  $CLASS_DEF_FILE > "$DEF_FILE" #combine all the temp def files into the main output file

#is wanted, add all the additional keywords
if [ $ADD_ADDITIONAL_KEYWORDS ]; then
    for keyword in $ADDITIONAL_KEYWORDS; do
	echo "\"$keyword\"," >> "$DEF_FILE"
	done
fi

sort "$DEF_FILE" | uniq > "$DEF_FILE" #sort the file and remove duplicates

#clean up the temporary files made
rm -f $FUNCTION_DEF_FILE
rm -f $VAR_DEF_FILE
rm -f $CLASS_DEF_FILE

exit 0